package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.utils.SharePrefenceTool;

public class Setup4Activity extends BaseSetupActivity {


    private CheckBox cb_proteting;
    private DevicePolicyManager dpm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup4);

        cb_proteting = (CheckBox) findViewById(R.id.cb_proteting);

        boolean protecting = SharePrefenceTool.getProtecting(this);
        if (protecting) {
            //手机防盗已经开启了
            cb_proteting.setText("手机防盗已经开启");
            cb_proteting.setChecked(true);
        } else {
            //手机防盗没有开启
            cb_proteting.setText("手机防盗没有开启");
            cb_proteting.setChecked(false);

        }

        cb_proteting.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    openAdmin();
                    cb_proteting.setText("手机防盗已经开启");
                } else {
                    cb_proteting.setText("手机防盗没有开启");
                }

                SharePrefenceTool.setProtecting(Setup4Activity.this, isChecked);

            }
        });
    }


    @Override
    public void showNext() {
        SharePrefenceTool.setFD(this, true);

        Intent intent = new Intent(this, LostFindActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_in, R.anim.tran_out);

    }

    @Override
    public void showPre() {
        Intent intent = new Intent(this, Setup3Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);

    }

    //显示激活页面，激活系统管理员
    private void openAdmin() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

        ComponentName mComponentName = new ComponentName(this, MyAdmin.class);

        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "get Protect need to open!");
        startActivity(intent);
    }

}
